package org.smartercontext.contextmger.utilities;

import java.util.ArrayList;
import java.util.List;

import org.smartercontext.contextmger.DTO.StudyDTO;
import org.smartercontext.contextmger.DTO.WorkDTO;
import org.smartercontext.contextmger.contextsphere.ContextManagerController;

import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;

public class GetWorkAndStudy {

	private List<WorkDTO>jobs;
	private List<StudyDTO>studies;
	
	public GetWorkAndStudy(){
		
	}
	
	public Model deleteWSToUserModel(Model usermodel, String username, String nameToDelete, String type, String time){
		
		String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
		Resource resource = usermodel.getResource(nsUser+username);
		Property studiedAt = usermodel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"studiedAt");
		Property studiesAt = usermodel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"studiesAt"); 
		Property workedAt = usermodel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"workedAt");
		Property worksAt = usermodel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"worksAt");
		
		
		if(type.equals("work")&&time.equals("Works At")){
			
			Statement st = null;
			
			StmtIterator iterator = resource.listProperties(worksAt);
			while(iterator.hasNext()){
				Statement stmt = iterator.next();
				String object = stmt.getObject().toString();
				if(object.equals(nsUser+nameToDelete)){
					st = stmt;
				}
			}
			
			if(st!=null){
				usermodel.remove(st);
			}
			
		}else if(type.equals("work")&&time.equals("Worked At")){
			
			Statement st = null;
			
			StmtIterator iterator = resource.listProperties(workedAt);
			while(iterator.hasNext()){
				Statement stmt = iterator.next();
				String object = stmt.getObject().toString();
				if(object.equals(nsUser+nameToDelete)){
					st = stmt;
				}
			}
			
			if(st!=null){
				usermodel.remove(st);
			}
			
		}else if(type.equals("study")&&time.equals("Studied At")){
			
			Statement st = null;
			
			StmtIterator iterator = resource.listProperties(studiedAt);
			while(iterator.hasNext()){
				Statement stmt = iterator.next();
				String object = stmt.getObject().toString();
				if(object.equals(nsUser+nameToDelete)){
					st = stmt;
				}
			}
			
			if(st!=null){
				usermodel.remove(st);
			}
			
		}else if(type.equals("study")&&time.equals("Studies At")){
			
			Statement st = null;
			
			StmtIterator iterator = resource.listProperties(studiesAt);
			while(iterator.hasNext()){
				Statement stmt = iterator.next();
				String object = stmt.getObject().toString();
				if(object.equals(nsUser+nameToDelete)){
					st = stmt;
				}
			}
			
			if(st!=null){
				usermodel.remove(st);
			}
		}
		
		
		
		return usermodel;
		
	}
	
	public Model addWSToUserModel(Model usermodel, String username){
		
		String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
		Resource resource = usermodel.getResource(nsUser+username);
		Property studiedAt = usermodel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"studiedAt");
		Property studiesAt = usermodel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"studiesAt"); 
		Property workedAt = usermodel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"workedAt");
		Property worksAt = usermodel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"worksAt");
		OntModel modelStudies = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
		DatatypeProperty studiesClasification = modelStudies.createDatatypeProperty(
				ContextManagerController.getNsSchemaPERSONAL()
				+ "studiesClassification");
		Property locatedIn = modelStudies.createDatatypeProperty(
				ContextManagerController.getNsSchemaGC()
				+ "locatedIn");
		Property rdfType = modelStudies.createProperty(ContextManagerController.getRdf(),"type");
		Property placeProp = modelStudies.createProperty(ContextManagerController.getNsSchemaGC(),"GroupEntity");
		DatatypeProperty geoLocationClassification = 
				modelStudies.createDatatypeProperty(ContextManagerController.getNsSchemaGC()
						+ "geoLocationClassification");
		Property geoPropr = modelStudies.createProperty(ContextManagerController.getNsSchemaGC(),"GeoLocation");
		
		for(int i=0;i<studies.size();i++){
			StudyDTO dto = studies.get(i);
			String name = dto.getSchoolName();
			String stype = dto.getSchoolType();
			String pORp = dto.getEndYear();
			
			Resource study = null;
			if(usermodel.getResource(nsUser+name)!=null){
				study = usermodel.getResource(nsUser+name);
				usermodel.remove(study.listProperties(studiesClasification));
			}else{
				study = modelStudies.createResource(nsUser+name);
			}
			
			Literal litClass = modelStudies.createTypedLiteral(new String(stype));
			usermodel.add(study,studiesClasification,litClass);

			usermodel.add(study,rdfType,placeProp);
			
			if(pORp!=null){
				
				usermodel.add(resource, studiedAt, study);
				
			}else{
				
				usermodel.add(resource, studiesAt, study);
				
			}
			
			
		}
		
		for(int i=0;i<jobs.size();i++){
			WorkDTO wdto = jobs.get(i);
			String name = wdto.getEmployerName();
			String loc = wdto.getEmployerLoc();
			String pORp = wdto.getEndDate();
			
			Resource work = null;
			
			if(usermodel.getResource(nsUser+name)!=null){
				work = usermodel.getResource(nsUser+name);
				usermodel.remove(work.listProperties(locatedIn));

			}else{
				work = modelStudies.createResource(nsUser+name);
			}
			
			
			if(loc!=null){
				
				Resource location = modelStudies.createResource(ContextManagerController.getNsCitiesModel()+loc);
				Literal place = modelStudies.createTypedLiteral(new String("City"));
				usermodel.addLiteral(location, geoLocationClassification, place);

				usermodel.add(work,rdfType,placeProp);
				usermodel.add(work,locatedIn,location);
								
			}
			
			if(pORp!=null){
				usermodel.add(resource,workedAt,work);
			}else{
				usermodel.add(resource,worksAt,work);
			}
		
		}
		
		return usermodel;
		
	}
	
	public void getStudiesAndWork(Model usermodel, String username){
		
		String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
		Resource resource = usermodel.getResource(nsUser+username);
		
		Property studiedAt = usermodel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"studiedAt");
		Property studiesAt = usermodel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"studiesAt"); 
		Property workedAt = usermodel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"workedAt");
		Property worksAt = usermodel.createProperty(ContextManagerController.getNsSchemaPERSONAL(),"worksAt");
		OntModel modelStudies = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
		DatatypeProperty studiesClasification = modelStudies.createDatatypeProperty(
				ContextManagerController.getNsSchemaPERSONAL()
				+ "studiesClassification");
		DatatypeProperty locatedIn = modelStudies.createDatatypeProperty(
				ContextManagerController.getNsSchemaGC()
				+ "locatedIn");
		
		
		studies = new ArrayList<>();
		jobs = new ArrayList<>();
		
		StmtIterator iteratorStudied = resource.listProperties(studiedAt);
		while(iteratorStudied.hasNext()){
			Statement stmt = iteratorStudied.next();
			String object = stmt.getObject().toString();
			
			Resource institute = usermodel.getResource(object);
			Statement stmt2 = institute.getProperty(studiesClasification);
			String classif=stmt2.getObject().asLiteral().getString();
			
			StudyDTO temp = new StudyDTO();
			temp.setSchoolName(object.split("#")[1]);
			temp.setSchoolType(classif);
			temp.setEndYear("x");
			studies.add(temp);
			
		}
		
		StmtIterator iteratorStudies = resource.listProperties(studiesAt);
		while(iteratorStudies.hasNext()){
			Statement stmt = iteratorStudies.next();
			String object = stmt.getObject().toString();
			
			Resource institute = usermodel.getResource(object);
			Statement stmt2 = institute.getProperty(studiesClasification);
			String classif=stmt2.getObject().asLiteral().getString();
			
			StudyDTO temp = new StudyDTO();
			temp.setSchoolName(object.split("#")[1]);
			temp.setSchoolType(classif);
			temp.setEndYear(null);
			studies.add(temp);
			
		}
		
		StmtIterator iteratorWorked = resource.listProperties(workedAt);
		while(iteratorWorked.hasNext()){
			Statement stmt = iteratorWorked.next();
			String object = stmt.getObject().toString();
			
			Resource institute = usermodel.getResource(object);
			Statement stmt2 = institute.getProperty(locatedIn);
			
			WorkDTO wDto = new WorkDTO();
			
			if(stmt2!=null){
			String locIn = stmt2.getObject().toString();
			if(locIn!=null){
				wDto.setEmployerLoc(locIn.split("#")[1]);
				}
			}
			
			wDto.setEmployerName(object.split("#")[1]);
			
			wDto.setEndDate("x");
			jobs.add(wDto);
						
		}
		
		StmtIterator iteratorWorks = resource.listProperties(worksAt);
		while(iteratorWorks.hasNext()){
			Statement stmt = iteratorWorks.next();
			String object = stmt.getObject().toString();
			
			Resource institute = usermodel.getResource(object);
			Statement stmt2 = institute.getProperty(locatedIn);
			
			WorkDTO wDto = new WorkDTO();
			
			if(stmt2!=null){
				if(stmt2.getObject()!=null){
				String locIn = stmt2.getObject().toString().split("#")[1];
				wDto.setEmployerLoc(locIn);
				}
			}
			
			wDto.setEmployerName(object.split("#")[1]);
			
			wDto.setEndDate(null);
			jobs.add(wDto);
						
		}
		
	}
	
	

	public List<WorkDTO> getJobs() {
		return jobs;
	}

	public void setJobs(List<WorkDTO> jobs) {
		this.jobs = jobs;
	}

	public List<StudyDTO> getStudies() {
		return studies;
	}

	public void setStudies(List<StudyDTO> studies) {
		this.studies = studies;
	}
	
	
}
